/* 频道管理模块的vuex操作 */
import { getUserChannels, getAllChannels } from '@/api/channel'

export default {
  namespaced: true,
  state: {
    channelList: [], // 用户频道
    allChannelList: []// 所有频道
  },
  mutations: {
    // 设置用户频道
    setChannelList(state, payload) {
      state.channelList = payload
    },
    // 添加用户频道
    addChannelList(state, payload) {
      state.channelList.push(payload)
    },
    // 删除用户频道
    delChannelList(state, payload) {
      state.channelList = state.channelList.filter(item => item.id !== payload.id)
    },
    // 设置所有频道
    setAllChannelList(state, payload) {
      state.allChannelList = payload
    }
  },
  actions: {
    // 用户频道
    async actions_getChannelList(context) {
      const res = await getUserChannels()
      context.commit('setChannelList', res.data.channels)
    },
    // 所有频道
    async actions_getAllChannelList(context) {
      const res = await getAllChannels()
      context.commit('setAllChannelList', res.data.channels)
    }
  },
  getters: {
    // 可选频道
    selChannelList(state) {
      return state.allChannelList.filter(item =>
        !state.channelList.some(obj => obj.id === item.id)
      )
    }
  }
}
